<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="90%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="巡查路线名称" prop="inspectionRouteName">
                <el-input v-model="ruleForm.inspectionRouteName" class="w100p" placeholder="请输入巡查路线名称"></el-input>
            </el-form-item>
            <el-form-item label="关联巡查点" prop="inspectionPointIdList">
                <el-select class="w100p" v-model="ruleForm.inspectionPointIdList" clearable filterable multiple
                    @change="handleChange">
                    <el-option v-for="item in pointList" :key="item.inspectionPointId" :label="item.inspectionPointName"
                        :value="item.inspectionPointId" />
                </el-select>
            </el-form-item>
            <el-form-item label="地图">
                <MapPoint ref="mapPoint" :closeClick="false"></MapPoint>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import patrolLineManage from "@/api/patrol-info-manage/patrol-line-manage";
import MapPoint from '@/components/MapPoint';
export default {
    components: { MapPoint },
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        pointList: {
            type: Array,
            default: () => {
                return []
            }
        }
    },
    data() {
        return {
            inspectionPointList: [],
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            pointListObj: {},
            rules: {
                inspectionRouteName: [
                    { required: true, message: '请输入巡查点名称', trigger: 'change' }
                ],
                inspectionPointIdList: [
                    { required: true, message: '请选择巡查点', trigger: 'change' }
                ]
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        },
        inspectionPointList: {
            handler(v) {
                let markerList = v.map(item => {
                    return {
                        point: [item.longitude, item.latitude],
                        markerName: item.inspectionPointName
                    }
                })
                this.$refs.mapPoint.addMarkerList(markerList)
            }
        }
    },

    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm();
                this.$set(this.ruleForm, 'inspectionRouteName', '');
                this.$set(this.ruleForm, 'inspectionPointIdList', []);
                this.addEditApi = patrolLineManage.addInspectionRouteInfo;
                this.addEditName = '添加';
                this.$refs.mapPoint.clearMarkers();
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data };
                this.addEditApi = patrolLineManage.editInspectionRouteInfo;
                this.addEditName = '编辑';
                this.queryPoint(this.ruleForm.inspectionRouteId);
                if (!this.ruleForm.inspectionPointIdList) {
                    this.$set(this.ruleForm, 'inspectionPointIdList', []);
                };
            }
            this.pointList.forEach(item => {
                this.pointListObj[item.inspectionPointId] = item
            })
        },
        async queryPoint(inspectionRouteId) {
            let inspectionPointIdList = []
            this.inspectionPointList = []
            this.$set(this.ruleForm, 'inspectionPointIdList', [])
            let { data: choosePointList } = await patrolLineManage.getDetailInspectionRouteById(inspectionRouteId)
            this.inspectionPointList = choosePointList.inspectionPointList || [];
            inspectionPointIdList = this.inspectionPointList.map(item => item.inspectionPointId)
            this.$set(this.ruleForm, 'inspectionPointIdList', inspectionPointIdList)
        },
        drawPoint(item) {
            if (!item.point) {
                this.$message.warning('请先选择巡查点')
            } else {
                this.$refs.mapPoint.toggleMarkerMode(item.point, [])
            }
        },
        handleChange(list) {
            this.inspectionPointList = [];
            list.forEach(id => {
                this.inspectionPointList.push(this.pointListObj[id])
            })
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        this.cancel();
                        this.$message[res.code == 1 ? 'success' : 'warning'](res.code == 1 ? '操作成功' : res.msg || '操作失败')
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            setTimeout(() => {
                this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
            }, 50);
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
    margin-top: 5vh !important;
}
</style>